<?php namespace App\Http\Controllers;

use Log;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Qiniu\Auth;
use App\Models\User;
use App\Utils\ReturnUtils;
use App\Models\Favorite;
use App\Models\BookRecord;
use App\Models\Attention;
use App\Models\Gift;

class UserController extends Controller
{
    
    //获取验证码
    public function getVerifyCode(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new User($request))->getVerifyCode();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //登陆-注册
    public function login(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new User($request))->login();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS'], $data);
    }
    
    //更新用户信息
    public function updateUserInfo(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new User($request))->updateUserInfo();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //获取用户信息
    public function getUserInfo(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new User($request))->getUserInfo();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS'], $data);
    }
    
    //用户收藏
    public function addFavorite(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new Favorite($request))->addFavorite();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //用户取消收藏
    public function delFavorite(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new Favorite($request))->delFavorite();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //获取收藏列表
    public function getFavoriteList(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new Favorite($request))->getFavoriteList();
        return $returnUtils->returnOk($returnUtils->return['SUCCESS'],$data['data'], $data['paging']);
    }
    
    //用户预定
    public function addBook(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new BookRecord($request))->addBook();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //用户取消预订
    public function delBook(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new BookRecord($request))->delBook();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //用户预定
    public function addAttention(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new Attention($request))->addAttention();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //用户取消预订
    public function delAttention(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new Attention($request))->delAttention();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS']);
    }
    
    //发礼物
    public function sendGift(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new Gift($request))->sendGift();
        if(!$data){
            return $returnUtils->returnError($returnUtils->return['ERROR_USER_GETINFO']);
        }
        return $returnUtils->returnOk($returnUtils->return['SUCCESS'],$data);
    }
    
    //获取用户历史记录
    public function getHistoryLog(Request $request) {
        $returnUtils = new ReturnUtils($request);
        $data = with(new User($request))->getHistoryLog();
        return $returnUtils->returnOk($returnUtils->return['SUCCESS'],$data['data'], $data['paging']);
    }
}
